home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 14.4 KB | 449 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // Geometry.cp
- // Copyright © 1985-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __GEOMETRY__
- #include "Geometry.h"
- #endif
-
- // Toolbox
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
-
- #pragma segment Main
-
- //========================================================================================
- // CLASS CPoint
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // Arithmatic operators for CPoint. Addition and subtraction are all that make any sense.
- // Both the Add and AddTo forms are defined.
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator+:
- //----------------------------------------------------------------------------------------
-
- CPoint CPoint::operator+(const CPoint& pt) const
- {
- return CPoint(h + pt.h, v + pt.v);
- } // CPoint::operator+
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator-
- //----------------------------------------------------------------------------------------
-
- CPoint CPoint::operator-(const CPoint& pt) const
- {
- return CPoint(h - pt.h, v - pt.v);
- } // CPoint::operator-
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator+=:
- //----------------------------------------------------------------------------------------
-
- CPoint& CPoint::operator+=(const CPoint& pt)
- {
- v += pt.v;
- h += pt.h;
- return *this;
- } // CPoint::operator+=
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator-=:
- //----------------------------------------------------------------------------------------
-
- CPoint& CPoint::operator-=(const CPoint& pt)
- {
- v -= pt.v;
- h -= pt.h;
- return *this;
- } // CPoint::operator-=
-
-
-
- //----------------------------------------------------------------------------------------
- // Relational operators for CPoint. These are defined by applying the operator in question
- // to both coordinates. The condition must hold for both to hold for the Points the
- // corresponding Points.
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator!=:
- //----------------------------------------------------------------------------------------
-
- Boolean CPoint::operator!=(const CPoint& pt) const
- {
- return v != pt.v || h != pt.h;
- } // CPoint::operator!=
-
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator==:
- //----------------------------------------------------------------------------------------
- Boolean CPoint::operator==(const CPoint& pt) const
- {
- return v == pt.v && h == pt.h;
- } // CPoint::operator==
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator>:
- //----------------------------------------------------------------------------------------
-
- Boolean CPoint::operator>(const CPoint& pt) const
- {
- return v > pt.v && h > pt.h;
- } // CPoint::operator>
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator<:
- //----------------------------------------------------------------------------------------
-
- Boolean CPoint::operator<(const CPoint& pt) const
- {
- return v < pt.v && h < pt.h;
- } // CPoint::operator<
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator>=:
- //----------------------------------------------------------------------------------------
-
- Boolean CPoint::operator>=(const CPoint& pt) const
- {
- return v >= pt.v && h >= pt.h;
- } // CPoint::operator>=
-
-
-
- //----------------------------------------------------------------------------------------
- // CPoint::operator<=:
- //----------------------------------------------------------------------------------------
- Boolean CPoint::operator<=(const CPoint& pt) const
- {
- return v <= pt.v && h <= pt.h;
- } // CPoint::operator<=
-
-
-
- //========================================================================================
- // CLASS CRect
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // Operators for adding and subtracting one CRect from to/ from another. Both the Add and
- // AddTo form of operators are defined.
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator+:
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator+(const CRect& rt) const
- {
- return CRect(left + rt.left, top + rt.top, right + rt.right, bottom + rt.bottom);
- } // CRect::operator+
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator-:
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator-(const CRect& rt) const
- {
- return CRect(left - rt.left, top - rt.top, right - rt.right, bottom - rt.bottom);
- } // CRect::operator-
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator+=:
- //----------------------------------------------------------------------------------------
-
- CRect& CRect::operator+=(const CRect& rt)
- {
- top += rt.top;
- left+= rt.left;
- bottom += rt.bottom;
- right += rt.right;
-
- return *this;
- } // CRect::operator+=
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator-=:
- //----------------------------------------------------------------------------------------
-
- CRect& CRect::operator-=(const CRect& rt)
- {
- top -= rt.top;
- left-= rt.left;
- bottom-= rt.bottom;
- right-= rt.right;
-
- return *this;
- } // CRect::operator-=
-
-
-
- //----------------------------------------------------------------------------------------
- // Operators for adding and subtracting a CPoint to/ from a CRect. A CPoint is added to a
- // CRect by adding the CPoint to both the top-left and bottom-right Points that define the
- // CRect. Both the Add and AddTo operators are defined. Very convenient for translating
- // Rects. These take a CPoint and since CPoint has a constructor that takes two shorts the
- // CRect windowRect can be translated 100 pixels in the positive y direction by the
- // statement:
- //
- // windowRect = windowRect + CPoint (0, 100);
- // or windowRect += CPoint (0, 100);
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator+
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator+(const CPoint& pt) const
- {
- return CRect(left + pt.h, top + pt.v, right + pt.h, bottom + pt.v);
- } // CRect::operator+
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator-:
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator-(const CPoint& pt) const
- {
- return CRect(left - pt.h, top - pt.v, right - pt.h, bottom - pt.v);
- } // CRect::operator-
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator+=:
- //----------------------------------------------------------------------------------------
-
- CRect& CRect::operator+=(const CPoint& pt)
- {
- top += pt.v;
- bottom+= pt.v;
- left+= pt.h;
- right+= pt.h;
-
- return *this;
- } // CRect::operator+=
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator-=:
- //----------------------------------------------------------------------------------------
-
- CRect& CRect::operator-=(const CPoint& pt)
- {
- top -= pt.v;
- bottom -= pt.v;
- left -= pt.h;
- right -= pt.h;
-
- return *this;
- } // CRect::operator-=
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Inset: Inset a CRect using the coordinates in CPoint for the inset delta
- //----------------------------------------------------------------------------------------
-
- CRect& CRect::Inset(const CPoint& delta)
- {
- top += delta.v;
- left += delta.h;
- bottom -= delta.v;
- right -= delta.h;
-
- return *this;
- } // CRect::Inset
-
-
-
- //----------------------------------------------------------------------------------------
- // Equality operators, other relational operator could be defined such as <. But their
- // meaning is ambiguous and probably better implemented as methods. For example, aRect <
- // bRect could return true if aRect was inside of bRect, or could return true if the area
- // of aRect was less than the area of bRect.
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator==
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::operator==(const CRect& rt) const
- {
- return
- top == rt.top && left == rt.left &&
- bottom == rt.bottom && right == rt.right;
- } // CRect::operator==
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator!=:
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::operator!=(const CRect& rt) const
- {
- return
- top != rt.top || left != rt.left ||
- bottom != rt.bottom || right != rt.right;
- } // CRect::operator!=
-
-
-
- //----------------------------------------------------------------------------------------
- // Two simple area operators, the macroIntersection & (bitwise and in C++) and the union |
- // (bitwise or in C++). The definition of union here is to return a CRect that exactly
- // encloses its operands.
- //----------------------------------------------------------------------------------------
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator&:
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator&(const CRect& rt) const
- {
- CRect returnRect (Max(left, rt.left), Max(top, rt.top), Min(right, rt.right), Min(bottom, rt.bottom));
-
- if (!returnRect.Valid())
- returnRect.top = returnRect.left = returnRect.bottom = returnRect.right = 0;
-
- return returnRect;
- } // CRect::operator&
-
-
- //----------------------------------------------------------------------------------------
- // CRect::operator|:
- //----------------------------------------------------------------------------------------
-
- CRect CRect::operator|(const CRect& rt) const
- {
- return CRect(Min(left, rt.left), Min(top, rt.top), Max(right, rt.right), Max(bottom, rt.bottom));
- } // CRect::operator|
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Empty: Returns true if the rectangle is empty.
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::Empty() const
- {
- return right <= left || bottom <= top;
- } // CRect::Empty
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Valid: Returns true if a valid rectangle (left < right and top < bottom). If not
- // a valid rectangle then returns false.
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::Valid() const
- {
- return left <= right && top <= bottom;
- } // CRect::Valid
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Validate: Fix up coordinates so that left <= right and top <= bottom.
- //----------------------------------------------------------------------------------------
-
- void CRect::Validate()
- {
- if (top > bottom)
- {
- short tmp = top;
- top = bottom;
- bottom = tmp;
- }
- if (left > right)
- {
- short tmp = left;
- left = right;
- right = tmp;
- }
- } // CRect::Validate
-
-
- //----------------------------------------------------------------------------------------
- // CRect::GetLength: Returns the length of a CRect in a given dimension.
- //----------------------------------------------------------------------------------------
-
- short CRect::GetLength(VHSelect sel) const
- {
- return (sel == vSel) ? (bottom - top) : (right - left);
- } // CRect::GetLength
-
-
- //----------------------------------------------------------------------------------------
- // CRect::GetSize: Returns the size of a CRect as a CPoint.
- //----------------------------------------------------------------------------------------
-
- CPoint CRect::GetSize() const
- {
- return CPoint(right - left, bottom - top);
- } // CRect::GetSize
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Contains(CPoint): This method takes a CPoint and returns true if is contained
- // within the CRect it is applied to
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::Contains (const CPoint& pt) const
- {
- // Does the CPoint 'pt' lie within the rectangle of 'this'?
-
- return pt.v >= top && pt.v < bottom && pt.h >= left && pt.h < right;
- } // CRect::Contains(CPoint)
-
-
- //----------------------------------------------------------------------------------------
- // CRect::Contains(CRect): This method takes a CRect and returns true if is contained
- // within the CRect it is applied to
- //----------------------------------------------------------------------------------------
-
- Boolean CRect::Contains (const CRect& rt) const
- {
- // Does the rectangle 'rt' lie withing the rectagle of 'this'?
-
- return rt.top >= top && rt.bottom <= bottom && rt.left >= left && rt.right <= right;
- } // CRect::Contains(CRect)
-
-
- //----------------------------------------------------------------------------------------
- // End of Geometry.cp
-
- #pragma segment Inline
-